package com.thesis.client;

import net.londatiga.android.ActionItem;
import net.londatiga.android.QuickAction;
import net.londatiga.android.QuickAction.OnActionItemClickListener;
import android.app.ActionBar;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.service.textservice.SpellCheckerService.Session;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.thesis.conectify.Connectify;
import com.thesis.object.ListUsers;
import com.thesis.utils.Defines;
import com.thesis.utils.Utils;

public class UserActivity extends Activity {

	ListUsers listUsers;
	ListView listview;
	int idUser;
	QuickAction aQuickAction;
	QuickAction mQuickAction;
	int u_pos = 0;
	String[] a = { "1", "2" };
	ProgressDialog pDialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_user);
		ActionBar ab = getActionBar();
		ab.setTitle(getResources().getString(R.string.title_activity_user));
		ab.setDisplayHomeAsUpEnabled(true);

		new LoadAllUser().execute();

		listview = (ListView) findViewById(R.id.list_user);

		// setListAdapter(new UserAdapter(UserActivity.this,USER));
		aQuickAction = new QuickAction(UserActivity.this);
		mQuickAction = new QuickAction(UserActivity.this);
		ActionItem detail = new ActionItem(0, "Chi tiết", getResources()
				.getDrawable(R.drawable.icon_user_detail));
		ActionItem edit = new ActionItem(1, "Sửa", getResources().getDrawable(
				R.drawable.icon_user_edit));
		ActionItem delete = new ActionItem(2, "Xóa", getResources()
				.getDrawable(R.drawable.icon_cancel));

		aQuickAction.addActionItem(detail);
		aQuickAction.addActionItem(edit);
		aQuickAction.addActionItem(delete);

		aQuickAction
				.setOnActionItemClickListener(new OnActionItemClickListener() {

					@Override
					public void onItemClick(QuickAction source, int pos,
							int actionId) {
						// TODO Auto-generated method stub
						switch (actionId) {
						case 0:
							String[] arr = {
									listUsers.getInstance().getListUser()
											.get(u_pos).getLastName(),
									listUsers.getInstance().getListUser()
											.get(u_pos).getFirstName(),
									listUsers.getInstance().getListUser()
											.get(u_pos).getUsername(),
									listUsers.getInstance().getListUser()
											.get(u_pos).getDateOfBirth(),
									listUsers.getInstance().getListUser()
											.get(u_pos).getAddress(),
									listUsers.getInstance().getListUser()
											.get(u_pos).getPhoneNumber(),
									Utils.sharedInstance().getNameManager(
											listUsers,
											listUsers.getInstance()
													.getListUser().get(u_pos)
													.getManagerId()),
									Integer.toString(listUsers.getInstance()
											.getListUser().get(u_pos).getRole()) };

							Intent intentDetail = new Intent(UserActivity.this,
									UserDetailActivity.class);
							intentDetail.putExtra(Defines.USER_DETAIL, arr);
							startActivity(intentDetail);
							break;
						case 1:
							String[] arr1 = {
									listUsers.getInstance().getListUser()
											.get(u_pos).getLastName(),
									listUsers.getInstance().getListUser()
											.get(u_pos).getFirstName(),

									listUsers.getInstance().getListUser()
											.get(u_pos).getDateOfBirth(),
									listUsers.getInstance().getListUser()
											.get(u_pos).getAddress(),
									listUsers.getInstance().getListUser()
											.get(u_pos).getPhoneNumber(),

									Integer.toString(listUsers.getInstance()
											.getListUser().get(u_pos).getId()) };
							Intent intentEdit = new Intent(UserActivity.this,
									UserEditActivity.class);
							intentEdit.putExtra(Defines.USER_DETAIL, arr1);
							startActivity(intentEdit);
							break;
						case 2:
							new AlertDialog.Builder(UserActivity.this)
									.setTitle("Xóa tài khoản")
									.setMessage("Bạn chắc chứ?")
									.setPositiveButton(
											"Xóa",
											new DialogInterface.OnClickListener() {
												public void onClick(
														DialogInterface dialog,
														int which) {
													idUser = listUsers
															.getInstance()
															.getListUser()
															.get(u_pos).getId();
													if (listUsers.getInstance()
															.getListUser()
															.get(u_pos)
															.getRole() != 0) {
														Utils.sharedInstance()
																.showShortToast(
																		UserActivity.this,
																		getResources()
																				.getString(
																						R.string.err_delete));
													} else {
														new DeleteUser()
																.execute();
													}

												}
											})
									.setNegativeButton(
											"Hủy",
											new DialogInterface.OnClickListener() {
												public void onClick(
														DialogInterface dialog,
														int which) {
													// do nothing
												}
											}).show();
							break;

						default:
							break;
						}
					}
				});
		
		mQuickAction.setOnActionItemClickListener(new OnActionItemClickListener() {
			
			@Override
			public void onItemClick(QuickAction source, int pos, int actionId) {
				// TODO Auto-generated method stub
				switch (actionId) {
				case 0:
					String[] arr = {
							listUsers.getInstance().getListUser()
									.get(u_pos).getLastName(),
							listUsers.getInstance().getListUser()
									.get(u_pos).getFirstName(),
							listUsers.getInstance().getListUser()
									.get(u_pos).getUsername(),
							listUsers.getInstance().getListUser()
									.get(u_pos).getDateOfBirth(),
							listUsers.getInstance().getListUser()
									.get(u_pos).getAddress(),
							listUsers.getInstance().getListUser()
									.get(u_pos).getPhoneNumber(),
							Utils.sharedInstance().getNameManager(
									listUsers,
									listUsers.getInstance()
											.getListUser().get(u_pos)
											.getManagerId()),
							Integer.toString(listUsers.getInstance()
									.getListUser().get(u_pos).getRole()) };

					Intent intentDetail = new Intent(UserActivity.this,
							UserDetailActivity.class);
					intentDetail.putExtra(Defines.USER_DETAIL, arr);
					startActivity(intentDetail);
					break;

				default:
					break;
				}
			}
		});
		listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View v,
					int position, long id) {
				// TODO Auto-generated method stub
				u_pos = position;
				if (com.thesis.object.Session.getInstance().getRole() == 2)
				{
					aQuickAction.show(v);
				} else {
					mQuickAction.show(v);
				}
			}

		});
	}

	// @Override
	// public boolean onCreateOptionsMenu(Menu menu) {
	// // Inflate the menu; this adds items to the action bar if it is present.
	// getMenuInflater().inflate(R.menu.user, menu);
	// return true;
	// }
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			finish();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == 100) {
			// if result code 100 is received
			// means user edited/deleted user
			// reload this screen again
			Intent intent = getIntent();
			finish();
			startActivity(intent);
		}
	}

	@Override
	protected void onDestroy() {
		listUsers.getInstance().clear();
		super.onDestroy();
	}

	class LoadAllUser extends AsyncTask<String, String, String> {
		// @Override
		// protected void onPreExecute() {
		// super.onPreExecute();
		// pDialog = new ProgressDialog(UserActivity.this);
		// pDialog.setMessage("Loading products. Please wait...");
		// pDialog.setIndeterminate(false);
		// pDialog.setCancelable(false);
		// pDialog.show();
		// }

		@Override
		protected String doInBackground(String... args) {
			// TODO Auto-generated method stub
			Connectify connect = new Connectify(UserActivity.this);
			listUsers = connect.getAllUsers();
			//connect.downloadUserImage();
			return null;
		}

		protected void onPostExecute(String file_url) {
			// dismiss the dialog after getting all products
			// pDialog.dismiss();
			// updating UI from Background Thread
			Log.d("listuser", "" + listUsers.getInstance().getListUser().size());
			listview = (ListView) findViewById(R.id.list_user);
			listview.setAdapter(new UserAdapter(UserActivity.this, listUsers));

		}

	}

	class DeleteUser extends AsyncTask<String, String, String> {

		@Override
		protected String doInBackground(String... params) {
			// TODO Auto-generated method stub
			Connectify connect = new Connectify(UserActivity.this);

			listUsers = connect.deleteUser(idUser);
			return null;
		}

		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			
			
			listview = (ListView) findViewById(R.id.list_user);
			listview.setAdapter(new UserAdapter(UserActivity.this, listUsers));
			
			Log.d("listuserapterdelte", ""
					+ listUsers.getInstance().getListUser().size());
		}
	}
}
